let db = require("../db/index");

// 收费项目列表查询
exports.chargeitems = (req, res) => {
    var sql =
        `select * from chargeitems`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
// 收费项目列表查询
exports.chargeitemsearch = (req, res) => {
    // console.log(req.body.value)
    let arr=[]
    req.body.value.forEach(item=>{
        var sql =
            `select * from chargeitems where projectname='${item}'`;
        db.query(sql, [], (err, data) => {
            if (err) {
                console.log("出错了", err);
                return res.send("错误" + err.message);
            }else{
                arr.push(...data)
            }
        });
    })
    
    setTimeout(()=>{
        res.send(arr)
    },500)
};
// 收费项目禁用
exports.chargeitemdisable = (req, res) => {
    var sql =
        `UPDATE chargeitems SET Disable='${req.body.value}',state='${req.body.value=='启用'?'禁用':'启用'}' WHERE items_id=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        console.log(sql)
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `select * from chargeitems`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send(data);
            });
        }
        
    });
};

// 收费项目删除
exports.chargeitemdel = (req, res) => {
    var sql =
        `DELETE FROM chargeitems WHERE items_id=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `select * from chargeitems`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send(data);
            });
        }
    });
};
// 收费项目添加
exports.chargeiteminsert = (req, res) => {
    var sql =
        `INSERT INTO chargeitems(projectname,projecttype,Billingmethod,billingcycle,note,state,edit,del,Disable)VALUES('${req.body.name}','${req.body.Category}','${req.body.region}','${req.body.billingcycle}个月','','启用','编辑','删除','禁用')`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};

// 抄表账单列表
exports.meterlists = (req, res) => {
    var sql =
        `select * from meter`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
// 添加抄表
exports.meterlistsinsert = (req, res) => {
    var sql =
        `INSERT into meter(Belongingcommunity,Buildingname,Numberofunits,RoomNo,changingitem,statementdate,cost,state,edit,del)VALUES('${req.body.community}','${req.body.building}',${req.body.unit},'${req.body.room}','${req.body.charingitem}','2021-06-01 ~2021-07-01','${req.body.unitprice*req.body.usage}','正常','编辑','删除')`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
// 删除抄表数据
exports.meterlistsdel = (req, res) => {
    var sql =
        `DELETE FROM meter WHERE meter_id=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `select * from meter`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send(data);
            });
        }
        
    });
};

// 费用统计查询
exports.costlists = (req, res) => {
    var sql =
        `select * from cost`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};